package com.zhz.lib.project.service.news;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.ibatis.dao.news.NewsDAO;
import com.zhz.lib.project.ibatis.domain.news.News;
import com.zhz.lib.project.vo.news.NewsVO;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午08:42:32
 * 
 */
@Service
public class NewsService {
    private static Logger  logger           = Logger.getLogger(NewsService.class);
    @Autowired
    private BaseDAO        baseDao;
    @Autowired
    private NewsDAO        newsDAO;

    // 最新消息类型
    private static Integer newstypefatherid = 1;
    private static int  MAX_LENGTH = 41;
    private static int	MAX_LENGTH_NEW = 36;

    /**
     * 获取最新消息
     * 
     * 最新消息的条件：<br/>
     * 1、未被删除 ifDelete =0<br/>
     * 2、审核通过aproveState=1<br/>
     * 3、发布时间早于当天<br/>
     * 
     * @param firstResult
     * @param maxResults
     * @return
     */
    public List<NewsVO> getLatestNews(int firstResult, int maxResults) {

        List<News> newsList = newsDAO.getLatestNews(firstResult, maxResults);
        List<NewsVO> newsVOList = new ArrayList<NewsVO>();
        int title_len=MAX_LENGTH_NEW;
        if (newsList != null && newsList.size() > 0){
        	int i=0;
            for (News news : newsList) {
                NewsVO newsVO = new NewsVO();
                BeanUtils.copyProperties(news, newsVO);
                if(i>3){
                	title_len=MAX_LENGTH;
                }
                newsVO.setShortTitle(ZhzStringUtil.subStringWithDot(newsVO.getTitle(), title_len));
                newsVOList.add(newsVO);
                i++;
            }
        }
        return newsVOList;
    }

    /**
     * 获取置顶的最新消息
     * 
     * 能够置顶的条件：<br/>
     * 1、未被删除 ifDelete =0<br/>
     * 2、设置为置顶 top=1<br/>
     * 3、审核通过 aproveState=1<br/>
     * 4、发布时间早于当天<br/>
     * 5、"当天"在置顶时间区域内<br/>
     * 
     * @param firstResult
     * @param maxResults
     * @return
     */
    public List<NewsVO> getTopNews(int firstResult, int maxResults) {

        List<News> newsList = newsDAO.getTopNews(firstResult, maxResults);
        List<NewsVO> newsVOList = new ArrayList<NewsVO>();
        if (newsList != null && newsList.size() > 0)
            for (News news : newsList) {
                NewsVO newsVO = new NewsVO();
                BeanUtils.copyProperties(news, newsVO);
                if (newsVO.getTitle() != null && newsVO.getTitle().getBytes().length > MAX_LENGTH)
                    newsVO.setTitle(ZhzStringUtil.subStringWithDot(newsVO.getTitle(), MAX_LENGTH));
                newsVOList.add(newsVO);
            }
        return newsVOList;
    }

    @SuppressWarnings("unchecked")
    public List<Sortmap> getNewsType() {

        // 查询数据库，得到最新消息类型
        return (List<Sortmap>) baseDao.findbyorderlimit("Sortmap", "fatherId", Integer
            .toString(newstypefatherid), "order by sortMapId ASC");
    }

    /**
     * 根据ID获取消息
     * 
     * @param hotlinkVO
     * @return
     */
    public NewsVO getNewsById(int id) {

        News news = newsDAO.getNewsById(id + "");
        NewsVO newsVO = new NewsVO();
        BeanUtils.copyProperties(news, newsVO);
        Sortmap newsfrom = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", news
            .getNewsfrom());
        newsVO.setNewsfrom(newsfrom.getValue());
        return newsVO;
    }

    /**
     * 点击次数增加1
     * 
     * @param hotlinkVO
     * @return
     */
    public void newsHitPlusOne(int id) {
        newsDAO.newsHitPlusOne(id + "");
    }
}
